package com.cjk.ssm.service.impl;

import com.cjk.ssm.entity.Department;
import com.cjk.ssm.exp.MyDeleteFailException;
import com.cjk.ssm.exp.MyNoDataException;
import com.cjk.ssm.exp.MySaveFailException;
import com.cjk.ssm.exp.MyUpdFailException;
import com.cjk.ssm.mapper.DepartmentMapper;
import com.cjk.ssm.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    private final int defultPageNum = 1;    // 默认页码
    private final int defultPageSize = 2;   // 默认每页显示条数
    private final int defultNavigatePages = 5;  // 默认导航分页的页码数
    private final String findMsg = "no data";   // 无数据时，提示消息
    private final String addMsg = "save fail";
    private final String delMsg = "delete fail";
    private final String updMsg = "update fail";


    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 添加一条记录
     * 只添加部门名称，id自动生成
     *
     * @param department
     * @return
     */
    @Override
    public void add(Department department) throws Exception {
        try {
            boolean res = departmentMapper.add(department);
            if (!res)
                throw new MySaveFailException(this.addMsg); // 添加失败
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 根据id，删除一条记录
     *
     * @param id
     * @return
     */
    @Override
    public void del(Integer id) throws Exception {
        try {
            boolean res = departmentMapper.del(id);
            if (!res) {
                throw new MyDeleteFailException(this.delMsg);
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 删除多条记录
     *
     * @param ids
     * @return
     */
    @Override
    public void delOneOrMore(Integer[] ids) throws Exception {
        try {
            boolean res = departmentMapper.delOneOrMore(ids);
            if (!res) {
                throw new MyDeleteFailException(this.delMsg);
            }
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 修改操作
     *
     * @param department
     * @return
     */
    @Override
    public void upd(Department department) throws Exception {
        try {
            boolean res = departmentMapper.upd(department);
            if (!res)
                throw new MyUpdFailException(this.updMsg);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 查找所有
     *
     * @return
     */
    @Override
    public List<Department> findAll() throws Exception {
        List<Department> departments;
        try {
            departments = departmentMapper.findAll();
            if (departments == null || departments.size() < 1) {
                throw new MyNoDataException(this.findMsg);
            }
        } catch (Exception e) {
            throw e;
        }
        return departments;
    }

    /**
     * 查找所有, 带有分页信息
     *
     * @param pageNume
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Department> findAllPage(Integer pageNume,
                                            Integer pageSize) throws Exception {
        if (pageNume == null || pageNume < 1)
            pageNume = this.defultPageNum;
        if (pageSize == null || pageSize < 1)
            pageSize = this.defultPageSize;

        PageInfo<Department> departmentPageInfo;
        try {
            //获取第pageNume页，pageSize条内容，默认查询总数count
            PageHelper.startPage(pageNume, pageSize);
            // 要执行的操作
            List<Department> departments = departmentMapper.findAll();
            if (departments == null || departments.size() < 1)
                throw new MyNoDataException(this.findMsg);
            //用PageInfo对结果进行包装
            departmentPageInfo = new PageInfo<>(departments, this.defultNavigatePages);
        } catch (Exception e) {
            throw e;
        }
        return departmentPageInfo;
    }

    /**
     * 查找所有, 带有分页信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<Department> findAllPage() throws Exception {
        return findAllPage(null, null);
    }

    /**
     * 根据id查找部门信息
     *
     * @param id@return
     */
    @Override
    public Department findById(Integer id) throws Exception {
        Department department;
        try {
            department = departmentMapper.findById(id);
            if (department == null) {
                throw new MyNoDataException(this.findMsg);
            }
        } catch (Exception e) {
            throw e;
        }
        return department;
    }

    /**
     * 根据姓名模糊查找
     *
     * @param name
     * @return
     */
    @Override
    public List<Department> findByDeptNameLike(String name) throws Exception {
        List<Department> departments;
        try {
            departments =
                    departmentMapper.findByDeptNameLike("%" + name + "%");
            if (departments == null || departments.size() < 1)
                throw new MyNoDataException(this.findMsg);
        } catch (Exception e) {
            throw e;
        }
        return departments;
    }

    /**
     * 根据名称模糊查找
     * 分页信息
     *
     * @param name
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<Department> findByDeptNameLikePage(String name) throws Exception {
        return null;
    }

    /**
     * 根据名称模糊查找
     * 分页信息
     *
     * @param name
     * @param pageNume
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<Department> findByDeptNameLikePage(String name, Integer pageNume, Integer pageSize) throws Exception {
        PageInfo<Department> departmentPageInfo;
        if (pageNume == null || pageNume < 1) {
            pageNume = this.defultPageNum;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = this.defultPageSize;
        }
        try {
            PageHelper.startPage(pageNume, pageSize);
            List<Department> departments = departmentMapper.findByDeptNameLike("%" + name + "%");
            System.out.println(""+departments);
            if (departments == null || departments.size() < 1) {
                throw new MyNoDataException(this.findMsg);
            }
//        将其封装到PageInfo对象中
            departmentPageInfo = new PageInfo<>(departments, this.defultNavigatePages);
            return departmentPageInfo;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 综合查找
     *
     * @param department
     * @return
     */
    @Override
    public List<Department> findByCondition(Department department) throws Exception {
        List<Department> departments;
        try {
            departments = departmentMapper.findByCondition(department);
            if (departments == null || departments.size() < 1)
                throw new MyNoDataException(this.findMsg);
        } catch (Exception e) {
            throw e;
        }
        return departments;
    }

    @Override
    public PageInfo<Department> findByConditionPage(Department department, Integer pageNum, Integer pageSize) throws Exception {
        return null;
    }

    @Override
    public PageInfo<Department> findByConditionPage(Department department) throws Exception {
        return null;
    }
}
